#ifndef HTTPBASE_INC
#define HTTPBASE_INC

#include "wch/Runnable.h"
using namespace wch;

#include "Common/TimeUtil.h"

#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/http.h>

struct ReqData
{
	struct evhttp_request * _req;
	UInt32 _time;

	ReqData()
		: _req(nullptr), _time(0)
	{}

	ReqData(struct evhttp_request * req_)
		: _req(req_), _time(TimeUtil::Now())
	{}
};

class HttpBase
	: public Runnable
{
public:
	HttpBase(UInt16);

public:
	bool init();
	void destroy();

public:
	void run();
	virtual void processReq(std::string, UInt32) {}
	virtual void processResp(UInt32, UInt8);
	void processRespImpl(std::vector<std::string>&);

public:
	static void generic_handler(struct evhttp_request *, void *);
	static void generic_timer(evutil_socket_t, short, void *);

protected:
	UInt16 _port;
	struct event_base * _base;
	struct evhttp * _server;
	struct timeval _tv;
	struct event * _timer;

protected:
	UInt32 _reqId;
	std::map<UInt32, ReqData> _reqs;
};

#endif